package com.spring.consumebackground.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.consumebackground.web.service.ConsumeBackgroundService;
import com.spring.security.core.annotation.OaRestPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;

@Controller
@RequestMapping(value = "/consumeBackground")
public class ConsumeBackgroundController {
	
	@Resource
	private ConsumeBackgroundService ConsumeBackgroundService;
	
	// 得到会员卡内容
	@RequestMapping(value = "/getCardDetail")
	@ResponseBody
	public Object getMemberCardDetail(String card_number,String phone, String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.getMemberCardDetail(card_number,phone);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}

	@RequestMapping(value = "/getMemberCardDetailList")
	@ResponseBody
	public Object getMemberCardDetailList(String card_number,String phone, String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.getMemberCardDetail(card_number,phone);
			m.get(ApplicationUtils.DATASET);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m.get(ApplicationUtils.DATASET));
		} else {
			return m.get(ApplicationUtils.DATASET);
		}
	}
	
	// 得到商品列表内容
	@RequestMapping(value = "/getRechargeCardDetail")
	@ResponseBody
	public Object getRechargeCardDetail(String card_number, String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.getRechargeCardDetail(card_number);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}

	@RequestMapping(value = "/getProductListByCode")
	@ResponseBody
	public Object getProductListByCode(String productCode,String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			System.out.println(productCode);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}		
			
	// 得到商品列表内容
	@RequestMapping(value = "/getProductList2")
	@ResponseBody
	public Object getProductList(String businessTypeCode,String page,String pageSize,String kids_park_id, String business_type_code, String code,
			String name,String deptID,String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		HashMap<String,Object>  hm=new HashMap<String,Object>();
		try {
			
			 hm = ConsumeBackgroundService.getProductList(businessTypeCode,page,pageSize,kids_park_id, code, name,deptID);
			 
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, hm);
		} else {
			return hm;
		}
	}

	//结账
	@RequestMapping(value = "/checkOut")
	@ResponseBody
	public Object checkOut(String assistID,String assistName,String c_user_id,String phone,String kids_park_id,String total_amount, String payment_amount, String desc, 
			String card_number, String order_detail, String order_payment,String cashierID,String cashierName,String callback) {

		
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.checkOut(assistID,assistName,null,c_user_id,phone,kids_park_id,total_amount, payment_amount, desc, card_number, order_detail, order_payment,0,null,null,cashierID,cashierName);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}
	
	//签到
	@OaRestPermission(value="/consumeBackground/courseSignIn")
	@RequestMapping(value = "/courseSignIn")
	@ResponseBody
	public Object courseSignIn(String card_number, String user_phone, String phone, String course_open_detailID,String callback,HttpServletRequest request) {
		Map<String, Object> m = new HashMap<String, Object>();
		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		String workID=oaUser.getOa_user_id();
		String workName=oaUser.getOa_user_name();
		
		try {
			m = ConsumeBackgroundService.courseSignIn(card_number, phone, course_open_detailID,workID,workName);

		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}
	
	//托班签到
	@OaRestPermission(value="/consumeBackground/careClassSignIn")
	@RequestMapping(value = "/careClassSignIn")
	@ResponseBody
	public Object careClassSignIn(String user_phone,String card_number, String phone,String babyID,String callback,HttpServletRequest request) {
		Map<String, Object> m = new HashMap<String, Object>();
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		String workID=oaUser.getOa_user_id();
		String workName=oaUser.getOa_user_name();
		
		try {
			m = ConsumeBackgroundService.careClassSignIn(user_phone, card_number, phone,workID,workName,babyID);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}

	@RequestMapping(value = "/careClassSignOff")
	@ResponseBody
	public Object careClassSignOff(String user_phone,String card_number, String phone, String care_class_openID,String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.careClassSignOff(user_phone, card_number, phone, care_class_openID);
					
		
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}
		
	@RequestMapping(value = "/careClassRewrite")
	@ResponseBody
	public Object careClassRewrite(String appointmentID, String specialRequirement, 
			String receiveUserID, String receiveUserName,String callback) {
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			m = ConsumeBackgroundService.careClassRewrite(appointmentID, specialRequirement, receiveUserID, receiveUserName);	
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}
	
	@RequestMapping(value = "/delOrder")
	@ResponseBody
	public Object delOrder(String orderID,String callback) {
		BaseResult br = new BaseResult();
		try {
			br = ConsumeBackgroundService.delOrder(orderID);	
		} catch (Exception e) {
			br=new BaseResult("0","系统异常");
			e.printStackTrace();
		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, br);
		} else {
			return br;
		}
	}

}

